package com.iwdnb.gkgz.application.kline;

import java.util.ArrayList;
import java.util.List;

import com.google.common.collect.Lists;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.common.utils.BigDecimalUtils;
import org.springframework.stereotype.Component;

/**
 * 神奇九转-高转卖出
 */
@Component
public class HighNineTurn implements Shape {

    @Override
    public List<StockDayData> getMatchData(String code, List<StockDayData> list,
        List<String> matchDateList) {
        List<StockDayData> stockDayDataList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            StockDayData stockDayData = list.get(i);
            if (match(code, stockDayData, Lists.newArrayList(list.subList(0, i)))) {
                stockDayDataList.add(stockDayData);
            }
        }
        return stockDayDataList;
    }

    @Override
    public boolean match(String code, StockDayData data, List<StockDayData> beforeDatas) {
        beforeDatas.add(data);
        if (beforeDatas.size() < 13) {
            return false;
        }
        List<StockDayData> datas = beforeDatas.subList(beforeDatas.size() - 13, beforeDatas.size());
        return highNineSell(datas);
    }

    /**
     * 高九卖出结构:满足两个条件：
     * 第一.即连续出现九根K线，并且这些K线的收盘价都比各自前面的第四根K线的收盘价高,就在其K线上方标记相应的数字，如果出现中断，则原计数作废,重新来过.
     * 第二：8或9的最高价大于6或7的最高价，在9的上方显示
     */
    private boolean highNineSell(List<StockDayData> list) {
        boolean isTurn = true;
        for (int i = 0; i < 9; i++) {
            StockDayData k0 = list.get(i);
            StockDayData k4 = list.get(i + 4);
            if (BigDecimalUtils.isLt(k4.getClosePrice(), k0.getClosePrice())) {
                isTurn = false;
            }
        }

        if (isTurn) {
            StockDayData nine = list.get(0);
            StockDayData eight = list.get(1);
            StockDayData seven = list.get(2);
            StockDayData six = list.get(3);

            if (BigDecimalUtils.isGt(nine.getMinPrice(), seven.getMaxPrice())
                || BigDecimalUtils.isGt(eight.getMinPrice(), six.getMaxPrice())) {
            } else {
                isTurn = false;
            }
        }
        return isTurn;

    }


}